package top.healthylife.gateway.platform.anxinsign.api.config;


import cn.hutool.core.codec.Base64Decoder;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import top.healthylife.basics.utils.exception.BusinessException;
import top.healthylife.basics.utils.utils.RedisUtil;
import top.healthylife.gateway.core.config.BaseConfig;
import top.healthylife.gateway.dto.annotation.SystemInfo;
import top.healthylife.gateway.dto.annotation.SystemInfoField;
import top.healthylife.gateway.dto.cons.DataPeriod;
import top.healthylife.gateway.platform.anxinsign.api.util.ExtEncryptAndDecrypt;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;

/**
 * 此接口服务需要哪些基础信息
 * 需要加入{@link SystemInfo}这个注解,方便在使用的过程中调用此服务功能
 * 注意：这个对象的使用，不要使用自动注入，请使用SystemConfigUtils 工具类进行获取对象，开发测试的时候可以使用注入形式
 *
 * @author GaoZexi
 */
@Slf4j
@SystemInfo(systemName = "安心签",periodValidity = DataPeriod.T1, shortName = "axsign")
@Data
@Configuration
@Accessors(chain = true)
@ConfigurationProperties(prefix = "system")
@SuppressWarnings("all")
public class SystemConfig extends BaseConfig implements Serializable {
    private static final long serialVersionUID = -4735148160159528848L;
    /**
     * 秘钥库密码
     */
    @SystemInfoField(value = "密码")
    String keystore;


    /**
     * pfx证书 配置管理中key
     */
    @SystemInfoField(value = "pfx证书")
    String pfxFileKey;

    /**
     * cer证书 配置管理中key
     */
    @SystemInfoField(value = "cer证书")
    String cerFileKey;

    /**
     * 秘钥库密码
     */
    @SystemInfoField(value = "机构编号")
    String institutionID;

    /**
     * 测试环境/正式环境
     */
    @SystemInfoField(value = "当前环境")
    Env env;

    /**
     * pfx证书文件流
     */
    InputStream pfxStream;

    /**
     * cer证书文件流
     */
    InputStream cerStream;


    /**
     * 加解密对象
     */
    ExtEncryptAndDecrypt extEncryptAndDecrypt;

    /**
     * 当前环境
     */
    public enum Env {
        /**
         * 测试环境
         */
        SIT,
        /**
         * 生产环境
         */
        PRO
    }

    public InputStream getPfxStream() {
        return getInputByFileKey(pfxFileKey);
    }
    //redis 存储文件时的格式,因为是前端的base64
    //String base64  = "data:application/x-x509-ca-cert;base64,LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tDQpNSUlFSGpDQ0F3YWdBd0lCQWdJRkVGa1ZRVUl3RFFZSktvWklodmNOQVFFRkJRQXdJVEVMTUFrR0ExVUVCaE1DDQpRMDR4RWpBUUJnTlZCQW9UQ1VOR1EwRWdUME5CTVRBZUZ3MHhOakV3TVRJd016STNOVGxhRncweU1URXdNVEl3DQpNekkzTlRsYU1JR0tNUXN3Q1FZRFZRUUdFd0pqYmpFU01CQUdBMVVFQ2hNSlEwWkRRU0JQUTBFeE1SWXdGQVlEDQpWUVFMRXcxRGFHbHVZVU5zWldGeWFXNW5NUlF3RWdZRFZRUUxFd3RGYm5SbGNuQnlhWE5sY3pFNU1EY0dBMVVFDQpBd3d3TURReFFEYzFOVEUwTkRNd056aEE1TGl0NlllUjVwU3Y1THVZNXJXTDZLK1Y1TGlUNTVTb1FEQXdNREF3DQpNREF5TUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUE4KytITmc5bXJuYmVNZkV1DQo0cWhEWmRlckRKR2hiUnFGZXN0SldHRzB1Rm9qY0R1cWxqY1pSTmtXSWp6R00yNVB4VXgvVVdmOU1HTzgrR2pnDQp5T3o4OXpNQXZvQ3V0Y0YxQm8rVk5oZlk0Tmtjb1VTKy9SOWxBREdFRURCTHJ1ZElXcldPdkxlblBteVplNEF1DQpramhCMlRWVnhWQVNURHFKYkp6bmlFUDJZNzAyWmVySEZGNHUwNU9PeGkxcVBhc3g1bWV0aXFQWU9EVWY2Ti9xDQpiOUMrVjFuTGsrRVZqTE0xWEJvZUJEenNmMVEzSW5jRk5TdmhLSWxrYnVua0NvUXJrRHU3UmZva2plV0RXbHR2DQpD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rUmhnDQpDclE9DQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tDQo";

    public InputStream getCerStream() {
        return getInputByFileKey(cerFileKey);
    }

    /**
     * 根据key获取文件流
     * @param fileKey
     * @return
     */
    private InputStream getInputByFileKey(String fileKey) {
        if(RedisUtil.exists(fileKey)){
            String base64 = RedisUtil.get(fileKey);
            int i = base64.indexOf("base64,");
            base64 = base64.substring(i + 7);
            log.debug("get file from redis [{}]:{}", fileKey, base64);
            byte[] decode = Base64Decoder.decode(base64);
            log.debug("length of decode : {}", decode.length);
            ByteArrayInputStream inputPfx = new ByteArrayInputStream(decode);
            return inputPfx;
        }
        log.error("配置不存在,请先去配置证书");
        throw new BusinessException("配置不存在,请先去配置证书");
    }
}
